package com.app.service.impl;

import cn.hutool.core.util.DesensitizedUtil;
import com.app.mapper.LotteryRecordMapper;
import com.app.model.BusinessModel;
import com.app.model.LotteryRecord;
import com.app.model.UserModel;
import com.app.service.BusinessService;
import com.app.service.ILotteryRecordService;
import com.app.service.UserService;
import com.app.utils.BetweenTimeUtils;
import com.app.vo.LotteryRecordVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 抽奖记录 服务实现类
 * </p>
 *
 * @author yoominic
 * @since 2023-12-16
 */
@Service
public class LotteryRecordServiceImpl extends ServiceImpl<LotteryRecordMapper, LotteryRecord> implements ILotteryRecordService {
    @Autowired
    @Lazy
    private UserService iUserService;
    @Autowired
    private BusinessService businessService;

    @Override
    public List<LotteryRecord> getNewRecords(Integer activityId) {
        List<LotteryRecord> newRecords = super.baseMapper.getNewRecords(activityId);
        for (LotteryRecord newRecord : newRecords) {
            Date dateNew1 = Date.from(newRecord.getDrawTime().atZone(ZoneId.systemDefault()).toInstant());
            String s = BetweenTimeUtils.BetweenUtils(dateNew1);
            newRecord.setDate(s);
            UserModel user = iUserService.getById(newRecord.getUserId());
            newRecord.setMobile(DesensitizedUtil.mobilePhone(user.getPhone()));
        }
        return newRecords;
    }

    @Override
    public LotteryRecordVo myLotteryRecord(Integer userId, Integer activityId, Integer type, Integer page, Integer limit) {
        page = (page - 1) * limit;
        List<LotteryRecord> newRecords = super.baseMapper.myLotteryRecord(userId, activityId, type, page, limit);
        Integer count = super.baseMapper.myLotteryRecordCount(userId, activityId, type);
        for (LotteryRecord newRecord : newRecords) {
            Date dateNew1 = Date.from(newRecord.getDrawTime().atZone(ZoneId.systemDefault()).toInstant());
            String s = BetweenTimeUtils.BetweenUtils(dateNew1);
            newRecord.setDate(s);
            UserModel user = iUserService.getById(newRecord.getUserId());
            newRecord.setMobile(DesensitizedUtil.mobilePhone(user.getPhone()));
            if(newRecord.getBussesType()!=null&&newRecord.getBussinessId()!=null)
            {
                if(newRecord.getIsSend()==1&&newRecord.getBussesType()==1)
                {
                    BusinessModel bussiness = businessService.getById(newRecord.getBussinessId());
                    if(bussiness!=null)
                    {
                        newRecord.setBussinessName(bussiness.getBusinessName());
                    }
                }
            }

        }
        LotteryRecordVo vo = new LotteryRecordVo();
        vo.setCount(count);
        vo.setList(newRecords);
        return vo;
    }

    @Override
    public Integer getUserTodayCount(Integer userId, Integer id) {
        return super.baseMapper.getUserTodayCount(userId, id);
    }
}
